import java.util.HashMap;
import java.util.Map;

public class TestUrlBuild {
    public static void main(String[] args) {
        // Test parameters
        String appKey = "hshxfnfofjfqfkfhfthxhyhxfgfqfxfqhxhohxfyfxfkfogvfihxhyhghghfhqhghhhohxftfgfshxhyhxgugggtgvhkhxhohxfhfjfifkhxhyhxfjhggpfhfyfxfbg{fdfohxhu";
        String answer = "1-0,2-1,3-1,4-1,5-1,6-1,7-0,8-1,9-1,10-0,11-1,12-0,13-0,14-0,15-1,16-0,17-1,18-1,19-1,20-1,21-1,22-0,23-1,24-0,25-1,26-0,27-0,28-0,29-0,30-1,31-0,32-0,33-1,34-0,35-1,36-0,37-1,38-1,39-1,40-1,41-1,42-0,43-1,44-0,45-0,46-0,47-0,48-0";
        
        Map<String, String> params = new HashMap<>();
        params.put("reportType", "3");
        params.put("answer", answer);
        params.put("appKey", appKey);
        params.put("tbl", "MBTI3");
        params.put("isAllAnswer", "1");
        
        String baseUrl = "https://php.bian-jie.cn/interface/bjsj/api/exam/getReport.php";
        String fullUrl = buildUrlWithParams(baseUrl, params);
        
        System.out.println("Built URL:");
        System.out.println(fullUrl);
        System.out.println();
        
        // Check if { is encoded to %7B
        if (fullUrl.contains("%7B")) {
            System.out.println("SUCCESS: { is encoded to %7B");
        } else {
            System.out.println("ERROR: { is not encoded");
        }
        
        // Check if answer contains commas (should not be encoded)
        if (fullUrl.contains("answer=1-0,2-1")) {
            System.out.println("SUCCESS: Commas in answer are not encoded");
        } else {
            System.out.println("ERROR: Commas in answer are encoded");
        }
    }
    
    private static String buildUrlWithParams(String baseUrl, Map<String, String> params) {
        if (params == null || params.isEmpty()) {
            return baseUrl;
        }

        StringBuilder urlBuilder = new StringBuilder(baseUrl);
        urlBuilder.append("?");

        boolean first = true;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (!first) {
                urlBuilder.append("&");
            }
            
            String key = entry.getKey();
            String value = entry.getValue() != null ? entry.getValue() : "";
            
            // Only encode curly braces, keep other characters as is
            String encodedValue = value.replace("{", "%7B").replace("}", "%7D");
            
            urlBuilder.append(key).append("=").append(encodedValue);
            first = false;
        }

        return urlBuilder.toString();
    }
}
